package com.jy.horny.adapter

import android.app.Activity
import androidx.viewpager2.widget.ViewPager2
import com.jy.chat.kit.utils.RouteUtils
import com.jy.horny.base.adpater.BaseRecyclerAdapter
import com.jy.horny.databinding.ItemHomeListBinding
import com.jy.horny.function.info.ProfileActivity
import com.jy.horny.model.HomeListModel
import com.jy.horny.utils.appContext
import com.lib.module_base.utils.AnimUtil
import com.lib.module_base.view.LinearLineIndicator
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.transformer.AlphaPageTransformer
import com.youth.banner.transformer.ScaleInTransformer
import com.youth.banner.util.BannerUtils
import io.rong.imlib.model.Conversation

class HomeListAdapter(var activity: Activity?) :
    BaseRecyclerAdapter<HomeListModel.MatchIndexBean, ItemHomeListBinding>(ItemHomeListBinding::inflate) {
    override fun onBindView(
        holder: VH<ItemHomeListBinding>,
        position: Int,
        item: HomeListModel.MatchIndexBean?
    ) {
        item?.apply {
            holder.binding.txtItemName.text = "${item.nickName},${item.age}"
            if (item.images.isNotEmpty()) {
                //配置图片
                val linearLineIndicator = LinearLineIndicator(appContext)
                if (item.images.size > 1) {
                    holder.binding.itemUserBanner.setUserInputEnabled(true)
                    linearLineIndicator.setIndicatorViewGravity(1)
                    holder.binding.itemUserBanner.indicator = linearLineIndicator
                    holder.binding.itemUserBanner.setIndicatorMargins(
                        IndicatorConfig.Margins(
                            0, BannerUtils.dp2px(10f), 0, 0
                        )
                    )
                }
                holder.binding.itemUserBanner.addPageTransformer(AlphaPageTransformer())
                holder.binding.itemUserBanner.addPageTransformer(ScaleInTransformer(0.7f))
                holder.binding.itemUserBanner.setAdapter(HomeImageAdapter(appContext, item.images))
                holder.binding.itemUserBanner.viewPager2.registerOnPageChangeCallback(object :
                    ViewPager2.OnPageChangeCallback() {
                    override fun onPageSelected(position: Int) {
                        linearLineIndicator.onPageSelected(position)
                        super.onPageSelected(position)
                    }
                })
//                holder.binding.leftClick.setOnClickListener {
//                    if (item.images.size > 1) {
//                        if (holder.binding.itemUserBanner.currentItem != 0) {
//                            holder.binding.itemUserBanner.currentItem =
//                                holder.binding.itemUserBanner.currentItem - 1
//                        } else {
//                            AnimUtil().shake(holder.itemView, appContext)
//                        }
//                    } else {
//                        AnimUtil().shake(holder.itemView, appContext)
//                    }
//
//                }
//                holder.binding.rightClick.setOnClickListener {
//                    if (item.images.size > 1) {
//                        if (holder.binding.itemUserBanner.currentItem != (item.images.size - 1)) {
//                            holder.binding.itemUserBanner.currentItem =
//                                holder.binding.itemUserBanner.currentItem + 1
//                        } else {
//                            AnimUtil().shake(holder.itemView, appContext)
//                        }
//                    } else {
//                        AnimUtil().shake(holder.itemView, appContext)
//
//                    }
//
//                }
            }
            holder.binding.txtItemChat.setOnClickListener {
                RouteUtils.routeToConversationActivity(
                    activity,
                    Conversation.ConversationType.PRIVATE,
                    item.friendUserCode
                )
                //移除选择的model
                getDataList().removeAt(position)
                notifyItemRemoved(position)
            }
            holder.binding.txtItemName.setOnClickListener {
                ProfileActivity.start(activity,item)
            }

        }
    }
}